﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MainDesk : MonoBehaviour {


    #region 数据字段

    private List<int> m_cardIds = new List<int>( );

    #endregion 数据字段

    void Start ( ) {
        NetHandle.Instance.EnterDeskReq(EnterDeskReturn);
        m_cardIds.Clear( );
    }

    #region 服务器响应

    private void EnterDeskReturn (ActionResult actionResult) {
        ActionResult[ ] record = actionResult.Get<ActionResult[ ]>("Children_1");

        for (int i = 0; i < record.Length; i++) {
            ActionResult subRecord = record[i];
            Debug.Log(string.Format("UserId={0},NickName={0},HeadIcon={0},PosId={0}",
                                                    subRecord.Get<int>("UserId"),
                                                    subRecord.Get<string>("NickName"),
                                                    subRecord.Get<string>("HeadIcon"),
                                                    subRecord.Get<int>("PosId")));
        }

        DealReq( );
    }

    private void DealReturn (ActionResult ar) {
        Debug.Log(string.Format("LandlordId={0},LandlordName={1},CodeTime={2}",
                                                ar.Get<int>("LandlordId"),
                                                ar.Get<string>("LandlordName"),
                                                ar.Get<int>("CodeTime")));

        TraveseCardId(ar, "Children_1");
        TraveseCardId(ar, "Children_2");

        CallLandlordReq(1);
    }

    private void CallLandlordResultReturn (ActionResult ar) {
        byte isEnd = ar.Get<byte>("IsEnd");
        Debug.Log(string.Format("IsEnd = {0},LandlordId={1},LandlordName={2},MultipleNum={3},AnteNum={4},IsCall={5},IsRob={6}",
            isEnd,
            ar.Get<int>("LandlordId"),
            ar.Get<string>("LandlordName"),
            ar.Get<int>("MultipleNum"),
            ar.Get<int>("AnteNum"),
            ar.Get<byte>("IsCall"),
            ar.Get<byte>("IsRob")));

        if (0 != isEnd) {
            CallLandlordReq(1);
        } else {
            MingPaiReq(1);
        }
    }

    private void MingPaiReturn (ActionResult ar) {
        ChuPaiInformReq( );
    }

    private void ChuPaiInformReturn (ActionResult ar) {
        Debug.Log(string.Format("UserId = {0},NextUserId={1},DeckType={2},CardSize={3}", ar.Get<int>("UserId"), ar.Get<int>("NextUserId"), ar.Get<short>("DeckType"), ar.Get<int>("CardSize")));

        ActionResult[ ] record = ar.Get<ActionResult[ ]>("Children_1");
        if (null == record || record.Length <= 0) return;

        for (int i = 0; i < record.Length; i++) {
            ActionResult subRecord = record[i];
            Debug.Log(string.Format("CardId = {0}", subRecord.Get<int>("CardId")));
        }
    }

    #endregion 服务器响应

    #region UGUI事件

    public void OnChuPai ( ) {
        if (m_cardIds == null || m_cardIds.Count <= 0) return;
        Debug.LogWarning("出牌ID=" + m_cardIds[0].ToString( ));
        NetHandle.Instance.ChuPaiReq(m_cardIds[0].ToString( ));
    }

    #endregion UGUI事件

    // 发牌
    private void DealReq ( ) {
        NetHandle.Instance.DealReq(DealReturn);
    }

    void TraveseCardId (ActionResult ar, string childrenName) {
        ActionResult[ ] record = ar.Get<ActionResult[ ]>(childrenName);
        if (null == record || record.Length <= 0) return;

        for (int i = 0; i < record.Length; i++) {
            ActionResult subRecord = record[i];            
            Debug.Log(string.Format("CardId = {0}", subRecord.Get<int>("CardId")));
            m_cardIds.Add(subRecord.Get<int>("CardId"));
        }
    }

    // 叫地主，1：叫，2：不叫
    void CallLandlordReq (int op) {
        NetHandle.Instance.CallLandlordReq(op);
    }

    void CallLandlordResultReq (byte isCall) {
        NetHandle.Instance.CallLandlordResultReq(isCall, CallLandlordResultReturn);
    }

    // 明牌接口 op 1：明牌，2：不明牌 
    void MingPaiReq (int op) {
        NetHandle.Instance.MingPaiReq(op, MingPaiReturn);
    }

    private void ChuPaiInformReq ( ) {
        NetHandle.Instance.ChuPaiInformReq(ChuPaiInformReturn);
    }

}
